var myScroll,
    pullDownEl, pullDownOffset,
    pullUpEl, pullUpOffset;
$(function() {
	//初始化下拉和上拉箭头
	pullDownEl = document.getElementById('pullDown');
    pullDownOffset = pullDownEl.offsetHeight;
    pullUpEl = document.getElementById('pullUp');  
    pullUpOffset = pullUpEl.offsetHeight;
	
    //拖拽刷新
	myScroll = new iScroll('wrapper',{
		scrollbarClass: 'myScrollbar',
		onRefresh: function () {
            if (pullDownEl.className.match('loading')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
            } else if (pullUpEl.className.match('loading')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
            }
        },
        onScrollMove: function () {
            if (this.y > 5 && !pullDownEl.className.match('flip')) {
                pullDownEl.className = 'flip';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';
                this.minScrollY = 0;
                $("#pullDown").show();
            } else if (this.y < 5 && pullDownEl.className.match('flip')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
                this.minScrollY = -pullDownOffset;
                $("#pullDown").show();
            } else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
                pullUpEl.className = 'flip';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';
                this.maxScrollY = this.maxScrollY;
                $("#pullUp").show();
            } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
                this.maxScrollY = pullUpOffset;
                $("#pullUp").show();
            }
        },
        onScrollEnd: function () {
            if (pullDownEl.className.match('flip')) {
                pullDownEl.className = 'loading';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';               
                pullDownAction();   // ajax call
            } else if (pullUpEl.className.match('flip')) {
                pullUpEl.className = 'loading';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';               
                pullUpAction(); // ajax call
            }
        }
	});
	
	
	
	//向上拖拽刷新
	function pullUpAction(){
		DiscountIndexApp.reload(DiscountIndexApp.getPage());
	}
	
	//向下拖拽刷新
	function pullDownAction(){
		DiscountIndexApp.reload(0);   
	}
	
	DiscountIndexApp.reload(0);
});
var DiscountIndexApp = function(){
	var pageSize = 6; //每页显示条数初始化，修改显示条数，修改这里即可
	var pCount = 0;
	var pagecur=0;
	return{
		getPage:function(){
			return pagecur;
		},
		mydiscount:function(){
			window.location.href=$("#APP_ROOT_PATH").val() + "discount/memberindex";
		},
		reload: function(page) {
			pagecur = page;
			var url = $("#APP_ROOT_PATH").val() + "discount/page_data";

			
				param = {
					"page": page + 1,
					"pageSize": pageSize
				};

			mui.util.Ajax.asyncGET(url, param, function(data) {
				pCount = data.total;
				var myDate = DiscountIndexApp.getTime();
				var json = {
					"data": data.rows,
					"path": $("#APP_ROOT_PATH").val(),
					"ossPath": mui.util.Enum.OSS_PATH,
					"date": myDate
				};
				var loaded = (page - 1) * pageSize + data.rows.length;
				if(data.rows.length <= 0|| loaded <= data.total){
					$(".bottom-1").text('没有更多活动啦');
				}else{
					$(".bottom-1").text('');
				}
				var bt = baidu.template;
				var htmlStr = bt('t:goodsList', json);
				
				if(page == 0){
					$(".goods_list").html(htmlStr);
				}else{
					$(".goods_list").append(htmlStr);
				}
//				if(pCount > 0) {
//					$("#Pagination").pagination(pCount, {
//						callback: DiscountIndexApp.reload,
//						prev_text: "« 上一页",
//						next_text: "下一页 »",
//						items_per_page: pageSize,
//						num_edge_entries: 2,
//						num_display_entries: 6,
//						current_page: page
//					});
//				} else {
//					$("#Pagination").html("");
//				}
				
				myScroll.refresh();
				$("#pullDown, #pullUp").hide();
				
				pagecur++;
			});
		},
		getTime:function getNowFormatDate() {
		    var date = new Date();
		    var seperator1 = "-";
		    var seperator2 = ":";
		    var month = date.getMonth() + 1;
		    var strDate = date.getDate();
		    if (month >= 1 && month <= 9) {
		        month = "0" + month;
		    }
		    if (strDate >= 0 && strDate <= 9) {
		        strDate = "0" + strDate;
		    }
		    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
		            + " " + date.getHours() + seperator2 + date.getMinutes()
		            + seperator2 + date.getSeconds();
		    return currentdate;
		},
		//立即参加活动
		joinnow:function(id){
				mui.util.Ajax.asyncGET(
						$("#APP_ROOT_PATH").val() + "discount/join", {
							"discountid": id
						},
						function(data) {
//							console.log(data);
							if(!isNaN(data)&&data >0) {
								layer.open({
									    content: '参与成功',
									    skin: 'msg',
									    time: 2 //2秒后自动关闭
								});
								window.location.href=$("#APP_ROOT_PATH").val() + "discount/memberdetails?discountid="+data;
							}else{
								layer.open({
										    content: data.message,
										    skin: 'msg',
										    time: 2 //2秒后自动关闭
									});
							}
						},
						function() {

						},
						function() {
							
						},
						function() {
						}
					)
		},
	}
}();